package edu.ahpu.propertymanage.util;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import edu.ahpu.propertymanage.pojo.Dispatch;
import edu.ahpu.propertymanage.pojo.Repair;
import edu.ahpu.propertymanage.pojo.Security;
import edu.ahpu.propertymanage.service.DispatchService;
import edu.ahpu.propertymanage.service.RepairService;
import edu.ahpu.propertymanage.service.SecurityService;
import edu.ahpu.propertymanage.service.WorkerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;

@Component
public class WorkerStarUtil {

    @Autowired
    WorkerService workerService;

    @Autowired
    DispatchService dispatchService;

    @Autowired
    RepairService repairService;

    @Autowired
    SecurityService securityService;

    public Float workerStar(String workerId) {


        //派工单
        QueryWrapper<Dispatch> disWrapper = new QueryWrapper();
        disWrapper.lambda().eq(Dispatch::getWorkerId, workerId);
        List<Dispatch> dispatches = dispatchService.list(disWrapper);

        //获取星级平均值
        float dCount = 0;
        float dStar = 0;
        for (Dispatch d : dispatches) {
            if (!("".equals(d.getDispatchSatLevel()) || d.getDispatchSatLevel() == null || d.getDispatchSatLevel() == 0)) {
                dCount++;
                dStar = dStar + d.getDispatchSatLevel();
            }
        }
        System.out.println("派工：星级：" + dStar + "，次数：" + dCount);

        //维修
        QueryWrapper<Repair> repWrapper = new QueryWrapper<>();
        repWrapper.lambda().eq(Repair::getWorkerId, workerId);
        List<Repair> repairs = repairService.list(repWrapper);
        //获取星级平均值
        float rCount = 0;
        float rStar = 0;
        for (Repair r : repairs) {
            if (!("".equals(r.getRepairSatLevel()) || r.getRepairSatLevel() == null || r.getRepairSatLevel() == 0)) {
                rCount++;
                rStar = rStar + r.getRepairSatLevel();
            }
        }

        System.out.println("维修：星级：" + rStar + "，次数：" + rCount);


        //安全检查
        QueryWrapper<Security> secWrapper = new QueryWrapper<>();
        secWrapper.lambda().eq(Security::getWorkerId, workerId);
        List<Security> securities = securityService.list(secWrapper);
        //获取星级平均值
        float sCount = 0;
        float sStar = 0;
        for (Security s : securities) {
            if (!("".equals(s.getSecuritySatLevel()) || s.getSecuritySatLevel() == null || s.getSecuritySatLevel() == 0)) {
                sCount++;
                sStar = sStar + s.getSecuritySatLevel();
            }
        }

        System.out.println("安全：星级：" + sStar + "，次数：" + sCount);


        //总平均值
        float counts = dCount + sCount + rCount;
        float stars = dStar + sStar + rStar;
        float star = 0;
        if (counts != 0)
            star = stars / counts;
        System.out.println("总星级：：" + stars + "，总次数" + counts);
        return star;
    }
}
